<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>创建一个基于JavaScript对象的Stack</title>
</head>
<script>
    class Stack {
        constructor() {
            this._count = 0;
            this._items = {};
        }
        push(element) {
            this._items[this._count++] = element;
        }
        size() {
            return this._count;
        }
        isEmpty() {
            return this._count === 0;
        }
        pop() {
            if (this.isEmpty()) {
                return undefined;
            }
            this._count--;
            const res = this._items[this._count];
            delete this._items[this._count];
            return res;
        }
        peek() {
            if (this.isEmpty()) {
                return undefined;
            }
            return this._items[this._count - 1];
        }
        clear() {
            this._items = {}
            this._count = 0;
            // while(!this.isEmpty()){
            //     this.pop();
            // }
        }
        toString() {
            if (this.isEmpty()) {
                return '';
            }
            let objString = `${this._items[0]}`;
            for (let i = 1; i < this._count; i++) {
                objString = `${objString},${this._items[i]}`;
            }
            return objString;
        }
    }
</script>

<body>

</body>

</html>